<?php
class ControllerSoftInventory extends Controller
{
	function index()
	{	
		$dt = getdate();
		$year = $dt['year'];
		$this->data['curentyear'] = $dt['year'];
		$this->data['curentmonth'] = $dt['mon'];
		for($i= $year - 10;$i<$year +10;$i++)
			$this->data["Year"][] =$i;
		for($i=1;$i<=12;$i++)
			$this->data["Month"][] =$i;
		$this->id='content';
		$this->template='soft/inventory.tpl';
		$this->layout='layout/center';
		$this->render();
	}
	
	public function getList()
	{
		$startdate=$this->date->formatViewDate($this->request->get['startdate']);
		$enddate=$this->date->formatViewDate($this->request->get['enddate']);
		$this->getListProduct($startdate,$enddate);
		
		$this->template='soft/inventoryproduct.tpl';
		$this->render();
	}
	
	public function getListProduct($startdate,$enddate)
	{
		$this->load->model("core/sitemap");
		$this->load->model("core/media");
		$this->data['category'] = array();
		$this->model_core_sitemap->getTree("category",$this->data['category']);
		
		foreach($this->data['category'] as $key => $item)
		{
			$deep = $this->model_core_sitemap->getDeep($item['sitemapid'],$this->user->getSiteId());
			$prefix = $this->string->getPrefix("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$deep-1);
			$this->data['category'][$key]['sitemapname'] = $prefix.$item['sitemapname'];
		}
		$ar = $this->string->matrixToArray($this->data['category'],"sitemapid");
		$where = " AND refersitemap in ('".implode("','",$ar)."')";
		$this->data['products'] = $this->model_core_media->getList($where);
		
		$listimportbegin = $this->getListOrder("",$startdate,"import");
		$arrimportbegin = $this->string->matrixToArray($listimportbegin,"orderid");
		
		$listimport = $this->getListOrder($startdate,$enddate,"import");
		$arrimport = $this->string->matrixToArray($listimport,"orderid");
		
		$listexportbegin = $this->getListOrder("",$startdate,"export");
		$arrexportbegin = $this->string->matrixToArray($listexportbegin,"orderid");
		
		$listexport = $this->getListOrder($startdate,$enddate,"export");
		$arrexport = $this->string->matrixToArray($listexport,"orderid");
		
		$listsalebegin = $this->getListOrder("",$startdate,"sale");
		$arrsalebegin = $this->string->matrixToArray($listsalebegin,"orderid");
		
		$listsale = $this->getListOrder($startdate,$enddate,"sale");
		$arrsale = $this->string->matrixToArray($listsale,"orderid");
		foreach($this->data['products'] as $key => $item)
		{
			$this->data['products'][$key]['title'] = str_replace("<br />","",$item['title']);
			
			$this->data['products'][$key]['importbegin'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrimportbegin);
			$this->data['products'][$key]['import'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrimport);
			
			$this->data['products'][$key]['exportbegin'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrexportbegin);
			$this->data['products'][$key]['export'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrexport);
			
			$this->data['products'][$key]['salebegin'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrsalebegin);
			$this->data['products'][$key]['sale'] = $this->model_addon_order->getTotalQuantity($item['mediaid'],$arrsale);
			
			$this->data['products'][$key]['inventorybegin'] = $this->data['products'][$key]['importbegin'] - $this->data['products'][$key]['exportbegin'] - $this->data['products'][$key]['salebegin'];
		}
	}
	
	function getListOrder($startdate,$enddate,$type)
	{
		$this->load->model("addon/order");
		$wherestart = "";
		$whereend = "";
		if($startdate !="")
			$wherestart = "AND orderdate >= '".$startdate."' ";
		if($enddate !="")
			$whereend = "AND orderdate  <= '".$enddate."'";
		$where = " AND `type` = '".$type."' 
					".$wherestart."
					".$whereend."
					ORDER BY  `order`.`orderdate` DESC ";
		
		$list = $this->model_addon_order->getListOrder($where);
		return $list;
	}
}
?>